<?php

namespace App\Http\Controllers;


use App\Member;
use App\Student;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller{
    public function test1 (){
//        $student = DB::select('select * from student');
//        var_dump($student);
        //新增数据
//        $bool = DB::insert('insert into student (name,age) value (?,?)',['Bob1',18]);
//        var_dump($bool);
        //更新数据
//        $res = DB::update('update student set age = ? where name = ?',['23','Bob']);
//        var_dump($res);exit;
        //有条件查询
//        $res = DB::select('select * from student where id > ?',[1]);
//        var_dump($res);exit;

        $res = DB::delete('delete from student where id = ?',['2']);

        var_dump($res);exit;

        return 'test 1';
    }
    public function query1()
    {
        //使用构造器 新增数据
//        $res = DB::table('student')->insert(['name'=>'alice','age'=>20]);
//        var_dump($res);exit;
        //获取insert得ID
//        $id = DB::table('student')->insertGetId(['name'=>'FuckSun','age'=>26]);
//        var_dump($id);exit;
        //插入多条数据
        $res = DB::table('student')->insert(
            [
                ['name'=>'alice2','age'=>46],
                ['name'=>'alice3','age'=>47],
                ['name'=>'alice4','age'=>48],
            ]
        );
        var_dump($res);exit;
        return 'test123';
    }

    public function query2 (){
        //更改指定的内容
//        $res = DB::table('student')->where('id',5)->update(['age'=>30]);

        //自增和自减
//        $res = DB::table('student')->increment('age');//age自增1
        $res = DB::table('student')
            ->where('id',3)
            ->decrement('age',3,['name'=>'baby']);//age自减1

        var_dump($res);exit;
        return 'test123';
    }

    public function query3(){

    //有条件删除
//        $res = DB::table('student')->where('id',4)->delete();
    //删除多条
    $res = DB::table('student')->where('id','>=',6)->delete();
    var_dump($res);exit;
    return 1;
}
    public function query4(){

        //查询方法
/***************************************************************/
        //get  获取表中得所有数据
//        $res = DB::table('student')->get();
        //first 获取表中得第一条数据
//        $res = DB::table('student')->first();
        //where 有条件查询
//        $res = DB::table('student')->where('id','>',2)->get();
        //where 多条件
//        $res = DB::table('student')->whereRaw('id >= ? and age >= ?',[4,30])->get();
        //pluck 返回结果集中制定得字段
//        $res = DB::table('student')->pluck('name');
        //lists 返回结果集中得字段 设置key=>value
//        $res = DB::table('student')->pluck('name','id')->toArray();
        //lists 选择获取的字段
//        $res = DB::table('student')->select('name','id')->get();
        //chunk  分段式查询
        $res = DB::table('student')->chunk(2,function ($students){var_dump($students);});//->get();

        return 1;
    }

    public function orm1 (){
        //all方法获取所有
        $res = Student::all();
        //find 获取ID = 得单条
        $res = Student::find(14);
        //get 获取所有
        $res = Student::get();
        //chunk
        $res = Student::chunk(2,function ($students){
            pr($students->toArray(),0);
        });

//        pr($arrs);
    }

    public function orm2 (){
        //使用模型新增
//        $student = new Student();
//        $student->name = 'fuckSun';
//        $student->age = '2';
//        $res = $student->save();
        //使用模型create 新增数据
        $res = Student::create(['name'=>'xiaoqiang','age'=>4]);
        pr($res);
    }

        //通过模型更新数据
    public function orm3 (){
        //更新单条记录
//        $res = Student::find(5);
//        $res->name = 'Hello';
//        pr($res->save());
//批量更新
        $res = Student::where('id','<','6')->update(['name'=>'world']);
        pr($res);
    }
    public function orm4(){
        //通过模型删除
//        $res = Student::find(5);
//        pr($res->delete());
        //主键删除
//        $res = Student::destroy('13');
//        $res = Student::destroy(['14','15']);
//        $res = Student::destroy('14','15');
        //删除指定条件
        $res = Student::where('id','>',19)->delete();
        pr($res);
    }

    public function view(){
        //视图使用
        $name = 'BBBBBBBBBBBB';
        return view('student/student',['name'=>$name]);
    }
    public function urlTest (){
        return 'URL TEST';
    }
}